import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_van_android/pages/home_page.dart';
import 'package:flutter_van_android/pages/login_page.dart';
import 'package:flutter_van_android/utils/router_manager.dart';

import '../utils/mmkv_util.dart';

class SplashScreen extends StatefulWidget with RouterBridge<void> {
  const SplashScreen({super.key});

  @override
  State<StatefulWidget> createState() {
    return _SplashScreenState();
  }
}

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();
    // _refreshConfig();
    _checkLoginStatus().then((isLoggedIn) {
      if (isLoggedIn) {
        // 如果已登录，跳转到主页
        if (!mounted) return;
        Navigator.pushReplacementNamed(
            context, RouterHelper.typeName(HomePage));
      } else {
        // 如果未登录，跳转到登录页面
        if (!mounted) return;
        Navigator.pushReplacementNamed(
            context, RouterHelper.typeName(LoginPage));
      }
    });
  }

  // 检查登录状态
  Future<bool> _checkLoginStatus() async {
    var token = MmkvUtil.getString("token", defaultValue: '');
    return token != null && token.isNotEmpty;
  }

  @override
  Widget build(BuildContext context) {
    // 这里你可以展示一个加载中的页面
    return Scaffold(
      body: Center(child: CircularProgressIndicator()),
    );
  }
}

void _refreshConfig() {
  EasyRefresh.defaultHeaderBuilder = () => ClassicHeader(
        dragText: '下拉刷新',
        armedText: '释放刷新',
        readyText: '正在刷新...',
        processingText: '正在刷新...',
        processedText: '刷新成功',
        noMoreText: '没有更多',
        failedText: '刷新失败',
        messageText: '上次更新于 %T',
      );
  EasyRefresh.defaultFooterBuilder = () => ClassicFooter(
        dragText: '上拉加载',
        armedText: '释放加载',
        readyText: '正在加载...',
        processingText: '正在加载...',
        processedText: '加载成功',
        noMoreText: '没有更多',
        failedText: '加载失败',
        messageText: '上次更新于 %T',
      );
}
